package com.socket.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;

/**
 * web socket的配置
 *
 * @author ww
 * @date 2018/7/5
 */
//@Configuration
//@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer{
    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        System.out.println("init web");
        registry.enableSimpleBroker("/topic");
        registry.setApplicationDestinationPrefixes("/app");

        //订阅Broker名称
        registry.enableSimpleBroker("/topic","/user");
        //全局使用的订阅前缀（客户端订阅路径上会体现出来）
        registry.setApplicationDestinationPrefixes("/app/");
        //点对点使用的订阅前缀（客户端订阅路径上会体现出来），不设置的话，默认也是/user/
        //registry.setUserDestinationPrefix("/user/");
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        System.out.println("init socketJs");
        //允许使用socketJs方式访问，访问点为hello，允许跨域
        registry.addEndpoint("/socketJs").setAllowedOrigins("*").withSockJS();
    }
}
